# -*- coding:utf-8 -*-
import pymysql.cursors
from itertools import chain, combinations
# 增加分类关联
if __name__ == '__main__':
    db2 = pymysql.connect(host='47.101.210.146',
                          user='woook_nest',
                          password='D27y3h4Lmr6knBpy',
                          database='woook_nest',
                          cursorclass=pymysql.cursors.DictCursor)
    with db2.cursor() as cursor:
        cursor.execute("SELECT `object_id`,`term_id` FROM `wp_term_ship` group by id")
        result = cursor.fetchall()

    big_l = {}
    for x in result:
        key = str(x['object_id'])
        big_l[key] = []

    for x in result:
        key = str(x['object_id'])
        big_l[key].append(x['term_id'])

    # 循环所有的组合
    comb = []
    for v in big_l:
        comb.append(list(combinations(big_l[v], 2)))

    comb = list(chain(*comb))

    insert_sql = 'INSERT INTO `wp_term_relationships` (term_id,target_id) VALUES (%s,%s)'
    with db2.cursor() as cursor:
        rows = cursor.executemany(insert_sql, comb)
        print(f'影响的rows {rows}')
        db2.commit()
        cursor.close()
